package com.finger.gulimall.coupon.service.impl;

import com.finger.common.to.MemberPrice;
import com.finger.common.to.SkuReductionTO;
import com.finger.gulimall.coupon.entity.MemberPriceEntity;
import com.finger.gulimall.coupon.entity.SkuLadderEntity;
import com.finger.gulimall.coupon.service.MemberPriceService;
import com.finger.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.Query;

import com.finger.gulimall.coupon.dao.SkuFullReductionDao;
import com.finger.gulimall.coupon.entity.SkuFullReductionEntity;
import com.finger.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private MemberPriceService memberPriceService;

    @Autowired
    private SkuLadderService skuLadderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveSkuReduction(SkuReductionTO skuReductionTO) {
        Long skuId = skuReductionTO.getSkuId();
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setSkuId(skuId);
        BigDecimal fullPrice = skuReductionTO.getFullPrice();
        BigDecimal reducePrice = skuReductionTO.getReducePrice();

        // 保存满减信息
        if (fullPrice != null || reducePrice != null) {

            if (fullPrice.compareTo(new BigDecimal("0")) == 1) {
                skuFullReductionEntity.setFullPrice(fullPrice);
            }

            if (reducePrice.compareTo(new BigDecimal("0")) == 1) {
                skuFullReductionEntity.setReducePrice(reducePrice);
            }

            skuFullReductionEntity.setAddOther(skuReductionTO.getPriceStatus());
            // 保存满减信息
            baseMapper.insert(skuFullReductionEntity);
        }

        // SkuLadderEntity
        int fullCount = skuReductionTO.getFullCount();
        BigDecimal discount = skuReductionTO.getDiscount();
        int countStatus = skuReductionTO.getCountStatus();
        if (fullCount > 0 && discount != null) {
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(skuId);
            skuLadderEntity.setFullCount(fullCount);
            skuLadderEntity.setDiscount(discount);
            skuLadderEntity.setAddOther(countStatus);
            skuLadderService.save(skuLadderEntity);
        }

        List<MemberPriceEntity> memberPriceEntities = skuReductionTO.getMemberPrice().stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setSkuId(skuId);
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).collect(Collectors.toList());

        if (memberPriceEntities.size() > 0) {
            // 保存会员价格
            memberPriceService.saveBatch(memberPriceEntities);
        }
    }

}